import { message } from "ant-design-vue";
import { defineStore } from "pinia";
import http from "../utils/http"
import {useHome} from "./home"

export const useComment = defineStore("comment", {
    state: () => {
        return {
            commentList: [],
            total:0
        }
    },
    actions: {
        //模糊搜素
        async getCommentList(obj) {
            let res = await http.get("/api/comment",{params:obj});
            if(res.status == 200){
                //处理后端返回的数据
                let newArr = res.data.data[0].map((v,i)=>{
                    v.createAt = new Date(v.createAt).toLocaleString()
                    v.key = i   
                    const tar = res.data.data[0].find(item=>item.id == v.parentCommentId)
                    v.prevntext =   (tar&&tar.name) || '无'
                    return v
                })
                this.commentList = newArr
                this.total = res.data.data[1]
            }
        },
        //更改评论状态
        async editcomment(obj,ishome = false){
            const homes = useHome()
            let res = await http.patch("/api/comment/"+obj.id,{pass:obj.pass});
            if(res.status===200){
                // message.success("更改成功")
                if(ishome){
                    homes.getList(1,6)
                }
            }
        },
        //删除
        async removecomment(id,ishome = false){
            const homes = useHome()
            let res = await http.delete("/api/comment/"+id);
            if(res.status===200){
                this.getCommentList({page:1,pageSize:12})
                if(ishome){
                    homes.getList(1,6)
                }
            }
        },
        //评论
        async addplList(obj){
            const homes = useHome()
            let res = await http.post("/api/comment",obj);
            if(res.status===201){
                this.getCommentList({page:1,pageSize:12})
                if(ishome){
                    homes.getList(1,6)
                }
            }
        }
    },
    getters: {

    }
})